﻿using MobileControlGuru.Base;
using MobileControlGuru.Src;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MobileControlGuru
{
    public partial class MutiPair : BaseForm
    {
        public MobileControlGuru.MainForm main { set; get; }
        public MutiPair(MobileControlGuru.MainForm _main)
        {
            this.main = _main;
            InitializeComponent();
            resources = new ComponentResourceManager(typeof(IPConnectMulti));
        }



        private void connect_btn_Click(object sender, EventArgs e)
        {



            int code = Convert.ToInt32(this.code_input.Text);
            string ipaddress = this.ip_input.Text;
            string portstring = this.port_input.Text;
            if (!string.IsNullOrEmpty(ipaddress) && !string.IsNullOrEmpty(portstring))
            {
                int port = Convert.ToInt32(this.port_input.Text);
                string[] iparray = ipaddress.Split(',');
                List<string> ips = new List<string>();
                List<Task> ts = new List<Task>();
                foreach (var ip in iparray)
                {
                    string ip_port = $"{ip}:{port}";
                    Task t = Task.Run(() =>
                    {
                        Console.WriteLine($"{DateTime.Now} {ip}:{port}");
                        var adbinfo = ADB.Pair(ip, port, code);
                        Console.WriteLine($"任务 {Task.CurrentId} 正在执行...{DateTime.Now}");

                    });
                  

                    ips.Add(ip_port);

                    ts.Add(t);
                }
                Task.WhenAll(ts);
               
               
                this.Close();
                // 所有任务完成后的后续操作
                Console.WriteLine("所有任务已完成。");





            }
        }

    }
}
